package com.bzs.batch.reconcile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bzs.batch.reconcile.batch.job.ConJob;
import com.bzs.batch.reconcile.entity.SysTransactionflow;
import com.bzs.batch.reconcile.mapper.SysTransactionflowMapper;
import com.bzs.batch.reconcile.service.ISysTransactionflowService;
import lombok.AllArgsConstructor;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author baozs
 * @since 2021-12-21
 */
@Service
@AllArgsConstructor
public class SysTransactionflowServiceImpl extends ServiceImpl<SysTransactionflowMapper, SysTransactionflow> implements ISysTransactionflowService {
    private final JobLauncher jobLauncher;
    private final ConJob conJob;
    private final SysTransactionflowMapper sysTransactionflowMapper;
    @Override
    public void dataToFile() {
        //开始任务之前把总金额和总笔数查出来放到job参数中
        QueryWrapper<SysTransactionflow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reconcil_result","01");
        queryWrapper.eq("push_status","00");
        List<SysTransactionflow> transactionflows = sysTransactionflowMapper.selectList(queryWrapper);
        int size = transactionflows.size();
        int batchAmount=0;
        for (SysTransactionflow transactionflow : transactionflows) {
            batchAmount+=Integer.parseInt(transactionflow.getTransAmount());
        }
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .addString("totalCount",String.valueOf(size))
                .addString("totalAmount",String.valueOf(batchAmount))
                .toJobParameters();
        try {
            jobLauncher.run(conJob.getSysTransJob(), jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
